local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHScreenBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 

local BigNumberHelper = ClientOHOH.load.BigNumberHelper();

local CommonOHOH =_G.CommonOHOH
local ConnectClass=CommonOHOH.load.Connect();
local Asset_Cfg=ClientOHOH.config.Asset
local GeneralTool=_G.CommonOHOH.load.GeneralTool();

function InitInsts(ui)
	ui.insts = {};
	ui.insts.root="Frame.Frame"
    ui.insts.cell="Frame.Frame.celll"
end

function M.MobileSize(self)
	if _G.client.IsMobile then --是移动设备
		if self.insts.root then
			local pos= self.insts.root:GetAttribute("Position")
			local size=self.insts.root:GetAttribute("Size")

			if pos then
				self.insts.root.Position=pos
			end
			if size then
				self.insts.root.Size=size
			end
		end
	end
end

function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.BuffGui"));
	self:MobileSize()
	self:InitAllButtons()
    self.CellList={}
end;

function M.InitAllButtons(self)
	self:AddProcess(function()
		for _, v in pairs(self.main_inst:GetDescendants()) do
			if v:IsA("GuiButton") then
				BtnHelper:InitButton(v, nil, nil, nil, function() end);
			end;
		end;
	end);
end
local gCellList={}

function M.CreatCellShow_old(self,item_id,time)
    if gCellList[item_id] then
        gCellList[item_id]:Add(time)
        return 
    end
    local cell= self.insts.cell:Clone()
    cell.Parent=self.insts.cell.Parent
    cell.Visible=true
    local image=cell:WaitForChild("ImageLabel")
    local timeLable=cell:WaitForChild("time")
    image.Image=Asset_Cfg[item_id].rid
    local obj={
        id=item_id,
        instance=cell,
        Image=image,
        TimeLable=timeLable,
        HadTime=time,
        UpdateTime=function(this,v)
           
            this.HadTime=this.HadTime+v
            this.TimeLable.Text=GeneralTool.secondsToHHMM(this.HadTime)
            if this.HadTime<0 then
        
                this:dtor()
                return true
            end
            return false
        end,
        Add=function(this,time)
            this.HadTime=this.HadTime+time
            this.TimeLable.Text=GeneralTool.secondsToHHMM(this.HadTime)
        end,
        dtor=function(this)
            gCellList[this.id]=nil
            this.instance:Destroy()
        end
    }
    obj:UpdateTime(0)
    self.CellList[obj]=true
    gCellList[item_id]=obj
    self:StartUpdate()
    return obj
end


function M.CreatCellShow(self,item_id,time)
    if gCellList[item_id] then
        gCellList[item_id]:Update(time)
        return gCellList[item_id]
    end
    local cell= self.insts.cell:Clone()
    cell.Parent=self.insts.cell.Parent
    cell.Visible=true
    local image=cell:WaitForChild("ImageLabel")
    local timeLable=cell:WaitForChild("time")
    image.Image=Asset_Cfg[item_id].rid
    local obj={
        id=item_id,
        instance=cell,
        Image=image,
        TimeLable=timeLable,
        Update=function(this,t)
            this.TimeLable.Text=GeneralTool.secondsToHHMM(t)
        end,
        dtor=function(this)
            gCellList[this.id]=nil
            this.instance:Destroy()
        end
    }
    obj:Update(time)
    self.CellList[obj]=true
    gCellList[item_id]=obj
    return obj
end


function M.StartUpdate(self)
    if not self.updateConnect then
        self.updateConnect=self:AddStepUpdate(self.Update,1)
    end
end

function M.Update(self)
    local keys={}
    for k,v in pairs(self.CellList) do
        if k:UpdateTime(-1) then
            table.insert(keys,k)
        end
    end

    for _,v in pairs(keys) do
        self.CellList[v]=nil
    end

    for k,v in pairs(self.CellList) do
        if v then
            return
        end
        
    end

    if self.updateConnect then
        self.updateConnect:Disconnect()
        self.updateConnect=nil
    end

end


function M.dtor(self)
    if self.updateConnect then
        self.updateConnect:Disconnect()
        self.updateConnect=nil
    end
	M.super.dtor(self);
end;


return M
